/**
 * 入库管理 - 查询入库记录
 * Created by zengtao on 2017/8/28.
 */
define(["jquery", "artTemplate","common", "text!../../tpls/rukuguanli/rukuguanlimain.html","text!../../tpls/rukuguanli/addruku.html","text!../../tpls/rukuguanli/addtuihuo.html","text!../../tpls/rukuguanli/ruku_tuihuo.html"], function ($, art,common, rukuguanlimain,addruku,addtuihuo,ruku_tuihuo) {
	var url = common.kcUrl(); 
	return function (begin_date,end_date,key_word) {
		$("#rkgl_wpxx_ul_svb").empty();
		var getDataBegin = layer.load(4, {time: 10*1000});
		//请求入库历史记录
		$.ajax({
			type: "POST",
			data:{
				type:0,
				begindate:begin_date || "",
				enddate:end_date || "",
				searchkey:key_word || ""
			},
			url: url + "stock/inbillList.shtml",
			success :function(res){
				layer.close(getDataBegin); 
				var result = eval('(' + res.result + ')');
				//console.log(result);
				if(result.length == 0){
					$("#rkgl_rklist_ul_svb").empty().append("<li class='list_empty'><span>暂无数据</span></li>");
					return
				}
				var rkList = art.render($("#tpl_rkgl_rkjl_listl").html(), {
					result: result   
				});					
				var $rkList = $(rkList);
				$("#rkgl_rklist_ul_svb").empty();
				$("#rkgl_rklist_ul_svb").append($rkList);
				setPaddingR("#rkgl_rklist_ul_svb",".rkjl_list",610);
				
				//左侧列表点击切换样式
				$('#rkgl_rklist_ul_svb').off("click").on("click",".list_jl_view_1503",function(){
					$(this).siblings().removeClass("list_active").end().addClass("list_active");
					var getDataBegin = layer.load(4, {time: 6*1000});
					
					//根据入库单号请求入库详细信息
					var sk_in_bill = $(this).find(".sk_in_bill_ipt").val();
					$.ajax({
						type: "GET",
						url: url + "stock/instockList.shtml?type=0&bill="+sk_in_bill,
						success: function(res){
							//console.log(res)
							layer.close(getDataBegin);
							var result = eval('(' + res.result + ')');
							var rkList = art.render($("#tpl_rkgl_rkwpxx_listr").html(), {
								result: result   
							});					
							var $rkList = $(rkList);
							$("#rkgl_wpxx_ul_svb").empty();
							$("#rkgl_wpxx_ul_svb").append($rkList);
							setPaddingR("#rkgl_wpxxlist_ul_svb",".rk_list_wpxx",610);
							
							//入库详细信息顶部全选按钮事件，改变样式及物品列表选择状态
							$("#selectAllSpan").off("click").on("click",function(){
								$(this).toggleClass("check_active");
								if(!$("#selectAllSpan").hasClass("check_active")){
									$("#selectAllIpt").prop("checked",false);
									$("#rkgl_wpxxlist_ul_svb input:checkbox").each(function(){
										$(this).prop("checked",false);
										$(this).parent().removeClass("check_active");
									});
									$("#rkgl_wpxxlist_ul_svb > li").removeClass("list_active");										
								}else{
									//console.log("hasClass(check_active)")
									//全选按钮选择状态
									$("#selectAllIpt").prop("checked","true");
									//物品信息列表每一列的input状态
									$("#rkgl_wpxxlist_ul_svb input:checkbox").each(function(){
										$(this).prop("checked",'true');
										$(this).parent().addClass("check_active");
									});
									//物品信息列表li标签样式
									$("#rkgl_wpxxlist_ul_svb > li").addClass("list_active");
								}								
							});

							//点击每项物品信息改变样式及选中状态
							$("#rkgl_wpxxlist_ul_svb > li").on("click",function(){
								var flag = true;
								//物品信息列表li标签样式								
								$(this).toggleClass("list_active");
								$(this).children(".rkgl_wpxxi_rkcheck").toggleClass("check_active");
								//物品信息列表每一列的input状态
								$("#rkgl_wpxxlist_ul_svb input:checkbox").each(function(){
										if($(this).parent().hasClass("check_active")){
											$(this).prop("checked",true);
										}else{
											flag = false;										
											$(this).prop("checked",false);
										}										
										// console.log($(this).is(':checked'))
									});	
									if(flag){
										$("#selectAllSpan").addClass("check_active");
										$("#selectAllIpt").prop("checked",true);
									}else{
										$("#selectAllSpan").removeClass("check_active");
										$("#selectAllIpt").prop("checked",false);
										
									}
							});
						}
					});
				});
				
				//入库记录 - 默认显示第一条入库单号的入库物品详细
				$('#rkgl_rklist_ul_svb .list_jl_view_1503').eq(0).trigger("click");
			},
			error:function(res){
				alert("网络异常，请稍后重试");
			},
			complete:function(XMLHttpRequest,textStatus){   
			     /* var sessionstatus=XMLHttpRequest.getResponseHeader("sessionstatus");
			      console.log(XMLHttpRequest);
			      console.log(textStatus);
			      console.log(sessionstatus);*/
			      if(textStatus=="timeout"){   
			    	  alert("网络异常，请稍后重试");  
			        }   
		      }   
		});
	};
});


